package com.review.sort;

public class InsertSort {
    //直接插入排序{1,2,3,4,5,6,7,9,10}
    public void insertSort(int[] array) {

        for (int i = 1; i < array.length; i++) {//遍历当前数组
            for (int j = i; j >= 0; j--) {//当前数不断与前面的数比较，符合就互换
                if (j - 1 >= 0 && array[j - 1] > array[j]) {//符合条件就互换
                    int tmp = array[j - 1];
                    array[j - 1] = array[j];
                    array[j] = tmp;
                }
            }
        }
    }

    public static void main(String[] args) {
        int[] ints = {8, 6, 7, 5, 9, 4, 1, 10, 2, 3};
        new InsertSort().insertSort(ints);
        for (int anInt : ints) {
            System.out.print(anInt + ",");
        }
    }
}
